package com.liyingjie.spzx.manager.task;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.liyingjie.spzx.manager.mapper.OrderInfoMapper;
import com.liyingjie.spzx.manager.mapper.OrderStatisticsMapper;
import com.liyingjie.spzx.model.entity.order.OrderStatistics;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Slf4j
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Scheduled(cron = "0 0 2 * * ?")  // 定义定时任务，使用@Scheduled注解指定调度时间表达式
    public void orderTotalAmountStatics() {
        //定义出前一天的日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        //查询订单详情的信息封装到订单状态表中
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        if(orderStatistics!=null){
            orderStatisticsMapper.insert(orderStatistics);
        }

    }

}